<template>
  <div class="app-container">
    <!--工具栏-->
    <div class="head-container">
      <div v-if="!crud.props.searchToggle">
        <el-form size="small" label-width="70px">
          <el-row :gutter="10">
<!--            <el-col :xs="24" :sm="12" :md="8" :lg="6" :xl="6">-->
<!--              <el-form-item label="客户名称">-->
<!--                <el-input></el-input>-->
<!--              </el-form-item>-->
<!--            </el-col>-->
<!--            <el-col :xs="24" :sm="12" :md="8" :lg="6" :xl="6">-->
<!--              <el-form-item label="销售部门">-->
<!--                <el-input></el-input>-->
<!--              </el-form-item>-->
<!--            </el-col>-->
<!--            <el-col :xs="24" :sm="12" :md="8" :lg="6" :xl="6">-->
<!--              <el-form-item label="销售人员">-->
<!--                <el-input></el-input>-->
<!--              </el-form-item>-->
<!--            </el-col>-->
<!--            <el-col :xs="24" :sm="12" :md="8" :lg="6" :xl="6">-->
<!--              <el-form-item label="客户分类">-->
<!--                <el-input></el-input>-->
<!--              </el-form-item>-->
<!--            </el-col>-->
<!--            <el-col :xs="24" :sm="12" :md="8" :lg="6" :xl="6">-->
<!--              <el-form-item label="客户区域">-->
<!--                <el-input></el-input>-->
<!--              </el-form-item>-->
<!--            </el-col>-->
            <el-col :xs="24" :sm="12" :md="8" :lg="6" :xl="6">
              <el-form-item label="年份">
                <el-select v-model="query.year" placeholder="请选择年份">
                  <el-option
                    v-for="item in years"
                    :key="item.value"
                    :label="item.value"
                    :value="item.value">
                  </el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :xs="24" :sm="12" :md="8" :lg="6" :xl="6">
              <el-button class="filter-item"  size="mini" type="primary" @click="crud.toQuery">搜索</el-button>
              <el-button class="filter-item" plain   size="mini" type="primary"
                         @click="crud.resetQuery()">重置</el-button>
<!--              <rrOperation :crud="crud"/>-->
            </el-col>
          </el-row>
        </el-form>
      </div>
      <!--表单组件-->
      <el-dialog :close-on-click-modal="false" :before-close="crud.cancelCU" :visible.sync="crud.status.cu > 0" :title="crud.status.title" width="500px">
        <el-form ref="form" :model="form" :rules="rules" size="small" label-width="80px">
          <el-form-item label="id">
            <el-input v-model="form.id" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="年份" prop="year">
            <el-input v-model="form.year" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="月份" prop="month">
            <el-input v-model="form.month" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="平均含税成本价">
            <el-input v-model="form.averageCostTaxPrice" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="平均未税成本价">
            <el-input v-model="form.averageCostNoTaxPrice" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="期初数量">
            <el-input v-model="form.beginGoodsCount" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="期初含税金额">
            <el-input v-model="form.beginTaxAmount" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="期初未税金额">
            <el-input v-model="form.beginNoTaxAmount" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="入库数量">
            <el-input v-model="form.inGoodsCount" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="入库金额">
            <el-input v-model="form.inTaxAmount" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="入库未税金额">
            <el-input v-model="form.inNoTaxAmount" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="出库数量">
            <el-input v-model="form.outGoodsCount" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="出库金额">
            <el-input v-model="form.outTaxAmount" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="出库未税金额">
            <el-input v-model="form.outNoTaxAmount" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="期末数量">
            <el-input v-model="form.endGoodsCount" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="期末含税金额">
            <el-input v-model="form.endTaxAmount" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="期末未税金额">
            <el-input v-model="form.endNoTaxAmount" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="是否结算(0:未结算,1:已结算)" prop="isSettled">
            <el-input v-model="form.isSettled" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="结算人id" prop="settleUserId">
            <el-input v-model="form.settleUserId" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="结算人名称">
            <el-input v-model="form.settleUserName" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="结算人编码">
            <el-input v-model="form.settleUserCode" style="width: 370px;" />
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="text" @click="crud.cancelCU">取消</el-button>
          <el-button :loading="crud.cu === 2" type="primary" @click="crud.submitCU">确认</el-button>
        </div>
      </el-dialog>
      <!--表格渲染-->
      <el-table ref="table" stripe border v-loading="crud.loading" :data="crud.data" size="small" style="width: 100%;" @selection-change="crud.selectionChangeHandler">
        <el-table-column type="selection" width="55" />
        <el-table-column type="index" width="55" label="序号"/>
<!--        <el-table-column v-if="columns.visible('id')" prop="id" label="id" />-->
        <el-table-column v-if="columns.visible('year')" prop="year" label="年份" />
        <el-table-column v-if="columns.visible('month')" prop="month" label="月份" />
<!--        <el-table-column v-if="columns.visible('averageCostTaxPrice')" prop="averageCostTaxPrice" label="平均含税成本价" />-->
<!--        <el-table-column v-if="columns.visible('averageCostNoTaxPrice')" prop="averageCostNoTaxPrice" label="平均未税成本价" />-->
<!--        <el-table-column v-if="columns.visible('beginGoodsCount')" prop="beginGoodsCount" label="期初数量" />-->
<!--        <el-table-column v-if="columns.visible('beginTaxAmount')" prop="beginTaxAmount" label="期初含税金额" />-->
<!--        <el-table-column v-if="columns.visible('beginNoTaxAmount')" prop="beginNoTaxAmount" label="期初未税金额" />-->
<!--        <el-table-column v-if="columns.visible('inGoodsCount')" prop="inGoodsCount" label="入库数量" />-->
<!--        <el-table-column v-if="columns.visible('inTaxAmount')" prop="inTaxAmount" label="入库金额" />-->
<!--        <el-table-column v-if="columns.visible('inNoTaxAmount')" prop="inNoTaxAmount" label="入库未税金额" />-->
<!--        <el-table-column v-if="columns.visible('outGoodsCount')" prop="outGoodsCount" label="出库数量" />-->
<!--        <el-table-column v-if="columns.visible('outTaxAmount')" prop="outTaxAmount" label="出库金额" />-->
<!--        <el-table-column v-if="columns.visible('outNoTaxAmount')" prop="outNoTaxAmount" label="出库未税金额" />-->
<!--        <el-table-column v-if="columns.visible('endGoodsCount')" prop="endGoodsCount" label="期末数量" />-->
<!--        <el-table-column v-if="columns.visible('endTaxAmount')" prop="endTaxAmount" label="期末含税金额" />-->
<!--        <el-table-column v-if="columns.visible('endNoTaxAmount')" prop="endNoTaxAmount" label="期末未税金额" />-->
        <el-table-column v-if="columns.visible('isSettled')"  prop="isSettled"
                         label="是否结算">
          <template slot-scope="scope" >
            {{ scope.row.isSettled?'已结算':'未结算' }}
          </template>
        </el-table-column>
<!--        <el-table-column v-if="columns.visible('settleUserId')" prop="settleUserId" label="结算人id" />-->
        <el-table-column v-if="columns.visible('settleUserName')" prop="settleUserName" label="结算人名称" />
        <el-table-column v-if="columns.visible('settleUserCode')" prop="settleUserCode" label="结算人编码" />
        <el-table-column v-if="columns.visible('settleTime')" prop="settleTime" label="结算时间" />
        <el-table-column v-permission="['admin','wareStockSettlement:edit','wareStockSettlement:del']" label="操作" width="150px" align="center">
          <template slot-scope="scope">
            <el-button @click="settle(scope.row.id)" type="text" size="small">结算</el-button>
            <el-button @click="unSettle(scope.row.id)" type="text" size="small">反结算</el-button>
          </template>
        </el-table-column>
      </el-table>
      <!--分页组件-->
<!--      <pagination />-->
    </div>
  </div>
</template>

<script>
import crudWareStockSettlement from '@/api/erp/wareStockSettlement'
import CRUD, { presenter, header, form, crud } from '@crud/crud'
import rrOperation from '@crud/RR.operation'
import crudOperation from '@crud/CRUD.operation'
import udOperation from '@crud/UD.operation'
import pagination from '@crud/Pagination'
import {getDict} from "../../../api/erp/tableColumnConfig";
import {settle,unSettle} from "@/api/erp/wareStockSettlement";

// crud交由presenter持有
const defaultCrud = CRUD({
  title: '库存结算',
  url: 'api/wareStockSettlement',
  sort: 'id,desc',
  query: {
    year: 2022,
  },
  crudMethod: {...crudWareStockSettlement}
})
const defaultForm = { id: null, year: null, month: null, averageCostTaxPrice: null, averageCostNoTaxPrice: null, beginGoodsCount: null, beginTaxAmount: null, beginNoTaxAmount: null, inGoodsCount: null, inTaxAmount: null, inNoTaxAmount: null, outGoodsCount: null, outTaxAmount: null, outNoTaxAmount: null, endGoodsCount: null, endTaxAmount: null, endNoTaxAmount: null, isSettled: null, settleUserId: null, settleUserName: null, settleUserCode: null }
export default {
  name: 'WareStockSettlement',
  components: { pagination, crudOperation, rrOperation, udOperation },
  mixins: [presenter(defaultCrud), header(), form(defaultForm), crud()],
  data() {
    return {
      permission: {
        add: ['admin', 'wareStockSettlement:add'],
        edit: ['admin', 'wareStockSettlement:edit'],
        del: ['admin', 'wareStockSettlement:del'],
        import: ['admin', 'wareStockSettlement:import'],
        export: ['admin', 'wareStockSettlement:export'],
        copy: ['admin', 'wareStockSettlement:copy'],
        upload: ['admin', 'wareStockSettlement:upload'],
        download: ['admin', 'wareStockSettlement:download'],
      },
      rules: {
        year: [
          { required: true, message: '年份不能为空', trigger: 'blur' }
        ],
        month: [
          { required: true, message: '月份不能为空', trigger: 'blur' }
        ],
        isSettled: [
          { required: true, message: '是否结算(0:未结算,1:已结算)不能为空', trigger: 'blur' }
        ],
        settleUserId: [
          { required: true, message: '结算人id不能为空', trigger: 'blur' }
        ]
      },
      years: []
    }
  },
  watch: {
  },
  created() {
    this.getYear()
    console.log(this.query.year)
    // console.log('==========')
    // console.log(this.year)
    // console.log(this.query.year)
  },
  methods: {
    settle(id){
      this.$confirm(`确认结算吗？`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        settle( id ).then(res => {
          this.$alert(res.msg, '提示')
          this.crud.toQuery()
        })
      }).catch(() => {
      })
    },
    unSettle(id){
      this.$confirm(`确认反结算吗？`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        // console.log( 'wareStockSettlement/'+ id)
        // this.$router.push({path: 'wareStockSettlement/' +id})
        unSettle( id ).then(res => {
          this.$alert(res.msg, '提示')
          this.crud.toQuery()
        })
      }).catch(() => {
      })
    },
    toQuery(){
      this.crud.toQuery()
      console.log('!!!!!!!!!')
      console.log(this.query.year)
    },
    async getYear(){
      const data = {dictCode: 'dict_order_year',size: 999}
      const years = await getDict(data)
      const year = new Date().getFullYear()
      this.years = years.content.filter(item=>item.value <= year)
      this.query.year = Number(year)
    },
    // 获取数据前设置好接口地址
    [CRUD.HOOK.beforeRefresh]() {
      return true
    }, // 新增与编辑前做的操作
    [CRUD.HOOK.afterToCU](crud, form) {
    },
  }
}
</script>

<style scoped>
  .table-img {
    display: inline-block;
    text-align: center;
    background: #ccc;
    color: #fff;
    white-space: nowrap;
    position: relative;
    overflow: hidden;
    vertical-align: middle;
    width: 32px;
    height: 32px;
    line-height: 32px;
  }
</style>
